package com.koala.data;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

@SuppressLint("NewApi")
public class MyUtils {
	private static final int STROKE_WIDTH = 10;
	private static Paint mPaint = new Paint();

	/**
	 * @param errorCode
	 *            -1 未知错误或填写的参数不足 -2 注册用户中必填信息不能为空 -3 注册用户的用户名必须在20个字内
	 *            -4注册用户的号码必须在20个数字 -5 注册用户的电话号码必须是号码 -6 注册用户邮箱地址长度必须在40内
	 *            -7注册用户的邮箱地址不和规范 -8 注册用户的密码长度在20个字符内 -9 密码必须为字母和数字
	 *            -10注册用户时用户名已存在 -11 注册用户时邮箱已存在 -12 token参数错误 -13 用户登陆失败
	 *            -14校验邮箱时用户名输入错误 -15 userKey参数错误 -16 用户的绑定邮箱与传入的邮箱参数不一致
	 *            -17协会信息调用失败 -18 获取公告失败，或数据库已无再多公告信息 -19 获取特享失败，或数据库已无再多特享信息
	 *            -20提交反馈信息失败 -21 提交反馈信息过长 -22 企讯通用户登陆失败 -24 企讯通的手机号码（账号）与密码不一致
	 *            -25qxtKey参数不一致，请重新登陆企讯通 -26 企讯通获取失败，或数据库已无再多企讯通信息
	 * @return 错误提示信息
	 */
	public static String getStringByErrorCode(int errorCode) {
		String errorHint = "网络或服务器异常，请尝试重新登录！";
		switch (errorCode) {
		case -1:
			errorHint = "遇到了未知错误，请反馈给管理员吧！";
			break;
		case -2:
			errorHint = "注册失败！缺少必填信息！";
			break;
		case -3:
			errorHint = "注册失败！用户名过长！";
			break;
		case -4:
			errorHint = "注册失败！电话号码过长！";
			break;
		case -5:
			errorHint = "注册失败！电话号码必需是数字！";
			break;
		case -6:
			errorHint = "注册失败！邮箱地址过长！";
			break;
		case -7:
			errorHint = "注册失败！邮箱地址不规范！";
			break;
		case -8:
			errorHint = "注册失败！密码过长！";
			break;
		case -9:
			errorHint = "注册失败！密码包含非法字符！";
			break;
		case -10:
			errorHint = "注册失败！用户名已存在！";
			break;
		case -11:
			errorHint = "注册失败！邮箱已被绑定！";
			break;
		case -12:
			errorHint = "应用错误！请跟管理员联系！";
			break;
		case -13:
			errorHint = "用户名或密码错误！";
			break;
		case -14:
			errorHint = "用户名与绑定邮箱不匹配！";
			break;
		case -15:
			errorHint = "登录已过期！请重新登录！";
			break;
		case -16:
			errorHint = "用户名与绑定邮箱不匹配！";
			break;
		case -17:
			errorHint = "协会信息提取失败！";
			break;
		case -18:
			errorHint = "获取公告失败，或数据库已无再多公告信息！";
			break;
		case -19:
			errorHint = "获取特享失败，或数据库已无再多特享信息！";
			break;
		case -20:
			errorHint = "提交反馈信息失败！";
			break;
		case -21:
			errorHint = "提交反馈信息过长！";
			break;
		case -22:
			errorHint = "帐号或密码错误！";
			break;
		case -24:
			errorHint = "帐号或密码错误！";
			break;
		case -25:
			errorHint = "企讯通登录已过期！请重新登录！";
			break;
		case -26:
			errorHint = "企讯通获取失败，或数据库已无再多企讯通信息！";
			break;
		default:
			break;
		}
		return errorHint;
	}

	public static <T> T[] arrayAdd(T[] first, T[] next) {
		if (first == null)
			return next;
		if (next == null)
			return first;
		T[] result = Arrays.copyOf(first, first.length + next.length);
		System.arraycopy(next, 0, result, first.length, next.length);
		return result;
	}

	@SuppressLint("NewApi")
	public static Uri toCircle(Uri src) {
		if (src == null)
			return null;
		String path = src.toString();
		Log.v("uri", path);
		File cache;
		if (!(Environment.getExternalStorageState())
				.equals(Environment.MEDIA_MOUNTED)) {
			return null;
		}
		cache = new File(Environment.getExternalStorageDirectory(),
				"Association/cache/circle");
		if (!cache.exists()) {
			cache.mkdirs();
		}
		String name = MD5.getMD5(path) + path.substring(path.lastIndexOf("."));
		File file = new File(cache, name);
		// 如果图片存在本地缓存目录，则不去服务器下载
		if (file.exists()) {
			return Uri.fromFile(file);// Uri.fromFile(path)锟斤拷锟斤拷锟斤拷锟斤拷艿玫锟斤拷募锟斤拷锟経RI
		} else {
			BitmapFactory.Options options = new BitmapFactory.Options();
			Bitmap btp = BitmapFactory
					.decodeFile(src.getEncodedPath(), options);
			Bitmap circle = toCircleBitmap(btp);
			try {
				FileOutputStream out = new FileOutputStream(file);
				circle.compress(Bitmap.CompressFormat.PNG, 90, out);
				out.flush();
				out.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return Uri.fromFile(file);
		}
	}

//	
//	private static Bitmap toCircleBitmap(Bitmap bitmap) {  // 锟斤拷锟絙itmap锟斤拷锟斤拷锟斤拷锟�锟斤拷锟斤拷锟斤拷new bitmap
////		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), 
////				bitmap.getHeight(), Config.ARGB_8888);
//		
//		int bmpWidth =  bitmap.getWidth() < bitmap.getHeight() ? bitmap
//				.getWidth() : bitmap.getHeight();
//		Bitmap output = Bitmap.createBitmap(bmpWidth,   //锟斤拷锟斤拷锟斤拷锟斤拷bitmap
//				bmpWidth, Config.ARGB_8888);
//		
//		
//		Canvas canvas = new Canvas(output);
//
//		final int color = 0xff000000;
//		
//		final Rect rect = new Rect(0, 0, bmpWidth, bmpWidth);
//		//final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
//		mPaint.setAntiAlias(true);
//		canvas.drawARGB(0, 0, 0, 0);
//		mPaint.setColor(color);
//		final int width = bitmap.getWidth() < bitmap.getHeight() ? bitmap
//				.getWidth() : bitmap.getHeight();
//				
//				canvas.drawCircle(bmpWidth /2 , bmpWidth / 2,
//						width / 2, mPaint);
////		canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
////						width / 2, mPaint);
//		mPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
//		canvas.drawBitmap(bitmap, rect, rect, mPaint);// 锟斤拷图片锟斤拷锟狡成帮拷色图片
//
//				// 锟斤拷锟斤拷色圆圈
//		/*mPaint.reset(); 
//		mPaint.setColor(Color.GREEN);
//		mPaint.setStyle(Paint.Style.STROKE);
//		mPaint.setStrokeWidth(STROKE_WIDTH); mPaint.setAntiAlias(true);
//		canvas.drawCircle(width / 2, width / 2, width / 2 - STROKE_WIDTH / 2,
//						mPaint);
//*/
//
//				return output;
//	}

	
	private static Bitmap toCircleBitmap(Bitmap bitmap) {
		final int width = bitmap.getWidth() < bitmap.getHeight() ? bitmap
				.getWidth() : bitmap.getHeight();
		final int height = bitmap.getHeight();
		Bitmap output = Bitmap.createBitmap(width,
				width, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff000000;
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final Rect torect = new Rect(0, 0, width,width);
		mPaint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		mPaint.setColor(color);
		
		canvas.drawCircle(width / 2, width / 2,
				width / 2, mPaint);
		mPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, torect, mPaint);// 将图片绘制成白色图片
		
		/* * // 画白色圆圈 mPaint.reset(); mPaint.setColor(Color.WHITE);
		 * mPaint.setStyle(Paint.Style.STROKE);
		 * mPaint.setStrokeWidth(STROKE_WIDTH); mPaint.setAntiAlias(true);
		 * canvas.drawCircle(width / 2, width / 2, width / 2 - STROKE_WIDTH / 2,
		 * mPaint);*/
		 
		return output;
		
		/*Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff000000;
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		mPaint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		mPaint.setColor(color);
		final int width = bitmap.getWidth() < bitmap.getHeight() ? bitmap
				.getWidth() : bitmap.getHeight();
		canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
				width / 2, mPaint);
		mPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, mPaint); 
		 
		return output;*/
		
	}
	
	
	public static <T> ArrayList<T> listAdd(ArrayList<T> first, ArrayList<T> next) {
		for (T temp : next) {
			first.add(temp);
		}
		return first;
	}

	public static <T> ArrayList<T> listConvert(ArrayList<T> oldList) {
		ArrayList<T> newList=new ArrayList<T>();
		for (int i=oldList.size();i>0;) {
			newList.add(oldList.get(--i));
		}
		return newList;
	}

	public static synchronized <T> ArrayList<T> listConvertAdd(ArrayList<T> first, ArrayList<T> next) {
		if(first==null||first.size()==0)
			return next ;
		
		try {
			for (int i=next.size();i>0;) {
				first.add(next.get(--i));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return first;
	}
}
